package javagamecontroller.activities.controllers;

import android.os.Vibrator;
import util.input.android.events.AndroidVibration;


/**
 * This thread will vibrate the controller
 * 
 * @author Ben Schwab
 * 
 */
public class AndroidVibrationThread extends Thread {

    private int myVibrationTime;
    private int myOnTime;
    private int myOffTime;
    private Vibrator myVibrator;
    private int myElapsedTime = 0;

    /**
     * Create a vibration event that will vibrate the controller based on the
     * vibration event parameter
     * 
     * @param vibration the vibration event describing the desired vibration
     * @param vibrator the vibrator of the controller
     */
    public AndroidVibrationThread (AndroidVibration vibration, Vibrator vibrator) {
        myVibrationTime = vibration.getVibrationTime();
        myOnTime = vibration.getOnTime();
        myOffTime = vibration.getOffTime();
        myVibrator = vibrator;
    }

    @Override
    public void run () {
        long sleep;

        while (myVibrationTime > myElapsedTime) {
            myVibrator.vibrate(myOnTime);
            sleep = myOnTime + myOffTime;

            try {
                Thread.sleep(sleep);
            }
            catch (InterruptedException e) {
                //a broken vibration isn't a big deal
            }
            myElapsedTime += sleep;
        }

    }

}
